package com.example.abe.diandi.trends.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.example.abe.diandi.R;
import com.example.abe.diandi.trends.model.TrendModel;

import example.abe.com.framework.imageloader.ImageLoader;
import example.abe.com.framework.imageloader.handle.BitmapHandle;
import example.abe.com.framework.recycleview.base.ViewHolder;

import static com.example.abe.diandi.trends.model.TrendModel.TYPE_IMAGE;

/**
 * Created by abe on 16/10/16.
 */
public class ImageTrendsDelegate extends BaseTrendsDelegate {

    private Context mContext;

    public ImageTrendsDelegate(OnClickListener listener){
        super();
        setOnClickListener(listener);
    }

    @Override
    public boolean isForViewType(TrendModel item, int position){
        if (item.getType() == TYPE_IMAGE){
            return true;
        }
        return false;
    }

    @Override
    protected void setInnerContent(Context context, FrameLayout contentView){
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.item_content_trends_image, contentView, false);
        contentView.addView(view);
    }

    @Override
    protected void bindDetailViewHolder(final ViewHolder holder, TrendModel t, int position){
        int i = 0;
        for (; i < t.getListImageTextIds().size() && i < 4; i++){
            final int curPosition = i;
            ImageLoader.getInstance().getBitmap(t.getListImageTextIds().get(curPosition).getImage(),
                    new BitmapHandle.BitmapHandleListener() {
                @Override
                public void onHandle(Bitmap bitmap) {
                    holder.setImageBitmap(getImageViewId(curPosition), bitmap);
                    holder.setVisible(getImageViewId(curPosition), true);
                }
            });
        }
        while (i<4){
            holder.setVisible(getImageViewId(i), false);
            i++;
        }
    }

    private int getImageViewId(int position){
        switch (position){
            case 0:
                return R.id.item_content_trends_iv_1;
            case 1:
                return R.id.item_content_trends_iv_2;
            case 2:
                return R.id.item_content_trends_iv_3;
            case 3:
                return R.id.item_content_trends_iv_4;
        }
        throw new ArrayIndexOutOfBoundsException("图片最多为四张！");
    }
}

